package lihua.robot.utils;

import lihua.robot.common.Result;
import lihua.robot.service.CrmWarehouseService;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.Objects;
import java.util.function.Function;

@Component
public class RedisUtil {

        public static void storeDataInCache(Integer devSn,Integer dqcd) {
       Jedis jedis = new Jedis("localhost", 6379);
       {
            int dbNumber = 3;
            jedis.select(dbNumber);
            if(jedis.get("3纺细纱"+devSn) != null){
                //从缓存中获取上一条数据
                String before = jedis.get("3纺细纱"+ devSn);
                TransportationConditionsUtil.fullBarrelCondition(dqcd, Integer.valueOf(before),devSn);
            }
            jedis.set("3纺细纱"+ devSn, String.valueOf(dqcd));
        }
    }

    /**
     * 机台当前长度置0
     */
    public static void set0(int devSn){
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            int dbNumber = 3;
            jedis.select(dbNumber);
            jedis.set("3纺细纱"+ devSn, String.valueOf(0));
        }
    }

    /**
     * 库位预约置1
     */
    public static void setKW1(String AP){
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            int dbNumber = 7;
            jedis.select(dbNumber);
            jedis.set(AP, "1");
        }
    }

    /**
     * 库位预约置0
     */
    public static void setKW0(String AP){
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            int dbNumber = 7;
            jedis.select(dbNumber);
            jedis.set(AP, "0");
        }
    }

    /**
     * 初始化库位预定信息
     */
    public static void Initialization(){
        String AP = "AP";
        for(int i=925;i<939;i++){
            try(Jedis jedis = new Jedis("localhost", 6379)){
                int dbNumber = 7;
                jedis.select(dbNumber);
                jedis.set(AP+i, String.valueOf(0));
            }
        }
    }

    /**
     * 初始化细纱当前长度信息
     */
    public static void InitializationXS(){
        String AP = "3纺细纱";
        for(int i=1;i<113;i++){
            try(Jedis jedis = new Jedis("localhost", 6379)){
                int dbNumber = 3;
                jedis.select(dbNumber);
                jedis.set(AP+i, String.valueOf(0));
            }
        }
    }

    /**
     * 追踪库位
     * type: 1 大桶满桶  2 大桶空桶  3 小桶满桶  4 小桶空桶
     * @return
     */
    public static String getWarehouselocation(int type,Integer deSvn) {
        CrmWarehouseService crmWarehouseService = (CrmWarehouseService) ApplicationContextUtil.getBean(CrmWarehouseService.class);
        int count = 0;
        Function<Integer, String> queryFunction = null;

        switch (type) {
            case 1:
                count = crmWarehouseService.queryFullBucketCapacityCount();
                queryFunction = i -> crmWarehouseService.queryFullBucketCapacity(i, deSvn);
                break;
            case 2:
                count = crmWarehouseService.queryEmptyBucketCapacityCount();
                queryFunction = i -> crmWarehouseService.queryEmptyBucketCapacity(i, deSvn);
                break;
            case 3:
                count = crmWarehouseService.queryXFullBucketCapacityCount();
                queryFunction = i -> crmWarehouseService.queryXFullBucketCapacity(i, deSvn);
                break;
            case 4:
                count = crmWarehouseService.queryXEmptyBucketCapacityCount();
                queryFunction = i -> crmWarehouseService.queryXEmptyBucketCapacity(i);
                break;
            default:
                return null;
        }

        try (Jedis jedis = new Jedis("localhost", 6379)) {
            int dbNumber = 7;
            jedis.select(dbNumber);
            for (int i = 0; i < count; i++) {
                String map = queryFunction.apply(i);
                if (Objects.equals(jedis.get(map), "0")) {
                    return map; // 直接返回找到的map
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 处理可能的异常
        }

        return null; // 如果没有找到符合条件的map，则返回null
    }


    /**
     * 小桶时间段内避免重复
     * @return
     */

    public static void setXT(Integer desvn){
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            int dbNumber = 8;
            jedis.select(dbNumber);
            jedis.set(String.valueOf(desvn), String.valueOf(desvn));
            jedis.expire(String.valueOf(desvn), 60*60*3);
        }
    }

    public static String getXT(Integer desvn){
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            int dbNumber = 8;
            jedis.select(dbNumber);
           return jedis.get(String.valueOf(desvn));
        }
    }

}
